Update for windows (WIP)
authorroot <root@localhost>
Tue, 2 Jun 2015 13:20:50 +0000 (15:20 +0200)
committerroot <root@localhost>
Tue, 2 Jun 2015 13:21:34 +0000 (15:21 +0200)
src/cargo/util/shell_escape.rs

index 39fb603bd39e18060873989fe82c57cbfb3ae577..9584794dae18b082d30562005dadb6d66679394a 100644 (file)
@@ -8,36 +8,54 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use std::borrow::Cow;
-
-static SHELL_SPECIAL: &'static str = r#" \$'"`!"#;
-
-/// Escape characters that may have special meaning in a shell,
-/// including spaces.
-pub fn shell_escape(s: Cow<str>) -> Cow<str> {
-    let escape_char = '\\';
-    // check if string needs to be escaped
-    let clean = SHELL_SPECIAL.chars().all(|sp_char| !s.contains(sp_char));
-    if clean {
-        return s
+#[cfg(not(target_os = "windows"))]
+pub use self::windows::shell_escape;
+
+
+mod windows {
+    use std::borrow::Cow;
+
+    pub fn shell_escape(s: Cow<str>) -> Cow<str> {
+        panic!()
     }
-    let mut es = String::with_capacity(s.len());
-    for ch in s.chars() {
-        if SHELL_SPECIAL.contains(ch) {
-            es.push(escape_char);
+}
+
+#[cfg(target_os = "windows")]
+pub use self::other::shell_escape;
+
+mod other {
+    use std::borrow::Cow;
+
+    static SHELL_SPECIAL: &'static str = r#" \$'"`!"#;
+
+    #[cfg(not(target_os = "windows"))]
+    /// Escape characters that may have special meaning in a shell,
+    /// including spaces.
+    pub fn shell_escape(s: Cow<str>) -> Cow<str> {
+        let escape_char = '\\';
+        // check if string needs to be escaped
+        let clean = SHELL_SPECIAL.chars().all(|sp_char| !s.contains(sp_char));
+        if clean {
+            return s
+        }
+        let mut es = String::with_capacity(s.len());
+        for ch in s.chars() {
+            if SHELL_SPECIAL.contains(ch) {
+                es.push(escape_char);
+            }
+            es.push(ch)
         }
-        es.push(ch)
+        es.into()
     }
-    es.into()
-}
 
-#[test]
-fn test_shell_escape() {
-    assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
-    assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()),
-                            r#"linker=gcc\ -L/foo\ -Wl,bar"#);
-    assert_eq!(shell_escape(r#"--features="default""#.into()),
-                            r#"--features=\"default\""#);
-    assert_eq!(shell_escape(r#"'!\$`\\\n "#.into()),
-                            r#"\'\!\\\$\`\\\\\\n\ "#);
+    #[test]
+    fn test_shell_escape() {
+        assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
+        assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()),
+                                r#"linker=gcc\ -L/foo\ -Wl,bar"#);
+        assert_eq!(shell_escape(r#"--features="default""#.into()),
+                                r#"--features=\"default\""#);
+        assert_eq!(shell_escape(r#"'!\$`\\\n "#.into()),
+                                r#"\'\!\\\$\`\\\\\\n\ "#);
+    }
 }